/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 日期处理
 *
 * @author Mark sunlightcs@gmail.com
 */
public class DateUtils {
    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";

    //yyyy-MM
    public final static String DATE_PATTERN_1 = "yyyy-MM";

    //yyyy/mm/dd
    public final static String DATE_PATTERN_2 = "yyyy/MM/dd";

    public final static String DATE_TIME_PATTERN_2 = "yyyyMMddHHmmss";

    public final static String DATE_TIME_PATTERN_3 = "yyyyMMddHHmm";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String ISO_DATE_TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'";


    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date 日期
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 日期解析
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回Date
     */
    public static Date parse(String date, String pattern) {
        try {
            return new SimpleDateFormat(pattern).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取昨日日期
     *
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String getYesterday(String pattern) {
        LocalDate lastDay = LocalDate.now().minusDays(1);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return lastDay.format(formatter);
    }

    public static String coverDate(String dateStr) {
        Date date = DateUtils.parse(dateStr, DateUtils.DATE_PATTERN);
        String formattedDate = DateUtils.format(date, DateUtils.DATE_PATTERN_2);
        return formattedDate;
    }

    /**
     * 将IOS时间转为指定格式字符串
     * @param
     */
    public static String coverISODate(String dateStr, String pattern) {
        DateTimeFormatter isoFormatter = DateTimeFormatter.ofPattern(ISO_DATE_TIME_PATTERN);
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, isoFormatter);
        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        String formattedDate = format(date, pattern);
        return formattedDate;
    }

    public static void main(String[] args) {
        String inputDateStr = "2025-01-13T07:46:55.000000Z";

        System.out.println(coverISODate(inputDateStr, DATE_TIME_PATTERN));
    }
}
